$(document).ready(function(){
	var waterPanelDate = $('.water-panel-date');
	var reservoirPanelSelect = $('.reservoir-panel-select');
	var waterDataTable = $('#waterDataTable');
	var shuikuBox=$('#shuikuBox');
	var outSide = $('#outSlideWater');
	
	var tableBodyTrs;
	var tableBodyFirstTds;
	var layer = $.map.layer,
		txtLayer = $.map.txtLayer,
		valLayer = $.map.valLayer,
		createMarkerSymbol = $.map.createMarkerSymbol;
	var water = $('#water');
	var hourTime_vertical = $('#hourTimeWater_vertical');
	hourTime_vertical.TouchSpin({
	      verticalbuttons: true,
	      min: 1,
	      max: 23,
	      initval:new Date().getHours()
	    });
	waterPanelDate.find('input[name="dayTimeWater"]').val($.format.date(new Date(),'yyyy-MM-dd'));
	// ZZ RR(SO、ST、BO、BT、MS、OZ) DD DP TT 
	var hedaos = xiaoyishuikus =xiaoershuikus=dayishuikus=daershuikus=zhongxingshuikus=qitashuiikus= yanzhas = bengzhans = chaoweis = {inSlide:[],outSlide:[]};
	function makeSymbol(url,width,height){
		return new esri.symbol.PictureMarkerSymbol(url, width, height);
	}
	$('#searchWaterStatBtn').on('click',function(){
		var findTrs;
		var searchKey = $('#searchWaterStatInput').val();
		var existsCls = waterDataTable.find('tbody tr.filterTr');
		var notExistsCls = waterDataTable.find('tbody tr').not('.filterTr');
		
		/*if(existsCls.length>0){
			findTrs = existsCls;
		}else{
			findTrs = waterDataTable.find('tbody tr').not(':hidden');
			findTrs.addClass('filterTr');
		}*/
		var hideTr = existsCls.filter(':not(:contains("'+searchKey+'"))');
		var showTr = existsCls.filter(':contains("'+searchKey+'")');
		$.each(showTr,function(index,item){
			$(item).find('td:eq(0)').text(parseInt(index)+1);
		});
		notExistsCls.hide();
		showTr.show();
		hideTr.hide();
	});
	
	function makerPointers(chooseTabs,isProv,dayTime){
		hourTime=hourTime_vertical.val();
		
		dayTime=new Date(dayTime).setHours(hourTime, 0, 0, 0);
		
		tm=$.format.date(dayTime,"yyyy-MM-dd HH:mm");
		$.get('../gis/WaterInfoScreen.json?method=getLatestRiverGisInfo',{
				tm:tm,r:new Date().getTime()
		},function(data){
			var waterDataTBody = waterDataTable.find('tbody');
			waterDataTBody.find('tr').remove();
			
			layer.clear();
			txtLayer.clear();
			valLayer.clear();
			map.graphics.clear();
			
			if (data.length>0) {
				var addIndex = 0;
				var sizeItem = data.length;
				var isChoose = outSide.prop('checked');
				var csvDatas = '序号,站名,数值,预警等级||';
				$.each(data,function(index,item){
				var sttp = item.sttp;
				var inProvOrInCity = item.inProvOrInCity;
				var val = parseFloat(item.val);
				var warValue = parseFloat(item.warValue);
				var warnLevel=item.warnLevel;
				var lgtd = parseFloat(item.lgtd);
				var lttd = parseFloat(item.lttd);
				var upAndDownVal = item.upAndDownVal;
				var temps,pngName = 'assets/';
				var callType;
				
				switch(sttp){
					case 'ZZ':
						temps = hedaos;
						pngName += 'ZZ';
						callType = 'river';
						break;
					case 'SO':
						temps = xiaoyishuikus;
						pngName += 'RR';
						callType = 'reservoir';
						break;
					case 'ST':
						temps = xiaoershuikus;
						pngName += 'RR';
						callType = 'reservoir';
						break;
					case 'BO':
						temps = dayishuikus;
						pngName += 'RR';
						callType = 'reservoir';
						break;
					case 'BT':
						temps = daershuikus;
						pngName += 'RR';
						callType = 'reservoir';
						break;
					case 'MS':
						temps = zhongxingshuikus;
						pngName += 'RR';
						callType = 'reservoir';
						break;
					case 'OZ':
						temps = qitashuiikus;
						pngName += 'RR';
						callType = 'reservoir';
						break;
					case 'DD':
						temps = yanzhas;
						pngName += 'DD';
						callType = 'weirgates';
						break;
					case 'DP':
						temps = bengzhans;
						pngName += 'DP';
						callType = 'pump';
						break;
					case 'TT':
						temps = chaoweis;
						pngName += 'TT';
						callType = 'tide';
						break;
				}
				if(inProvOrInCity == 0){
					temps.outSlide.push(item);
				}else if(inProvOrInCity == 1){
					temps.inSlide.push(item);
				}
				
				if(warValue>0 && warValue<val){
					pngName += '_RED';
				}
				
				pngName +='.png';
				
				var attr = {
					sttp : sttp,
					callType : callType,
					stcd : item.stcd,
					stnm : item.stnm,
					val : item.val,
					upAndDownVal : item.upAndDownVal,
					inProvOrInCity : item.inProvOrInCity,
					warnLevel :item.warnLevel,
					lgtd : lgtd,
					lttd : lttd
				}
				var csvData = (index + 1)+','+attr.stnm+','+attr.upAndDownVal+','+attr.warnLevel;
				if(index + 1 == sizeItem){
					csvDatas += csvData;
				}else{
					csvDatas += csvData+'||';
				}
				var valTextSymbol =new esri.symbol.TextSymbol(item.upAndDownVal);
				valTextSymbol.yoffset = 8;
				var textSymbol = new esri.symbol.TextSymbol(item.stnm+'('+item.upAndDownVal+')');
				textSymbol.yoffset = 8;
				
				var pngSymbol = makeSymbol(pngName,12,12);
				var point =  new esri.geometry.Point(lgtd,lttd,map.spatialReference);
				var pointGraphic = new esri.Graphic(point, pngSymbol, attr);
				var txtGraphic = new esri.Graphic(point, textSymbol);
				var pointValGraphic = new esri.Graphic(point, valTextSymbol);
				
				var tr = $('<tr class="filterTr"><td class="firstTd"></td><td>'+attr.stnm+'</td><td>'+attr.upAndDownVal+'</td><td>'+attr.warnLevel+'</td></tr>');
				tr.data('lgtd',attr.lgtd).data('lttd',attr.lttd).data('val',attr.upAndDownVal).data('stnm',attr.stnm).data('stcd',attr.stcd)
				.data('sttp',sttp).data('callType',callType);
				if($.inArray(sttp,chooseTabs)!=-1){
					if((isProv && inProvOrInCity == 0) || inProvOrInCity == 1){// 选中省外或省内
						pointGraphic.show();
						txtGraphic.show();
						pointValGraphic.show();
						tr.find('td:eq(0)').text(++addIndex);
					}else{// 为选中省外
						pointGraphic.hide();
						txtGraphic.hide();
						pointValGraphic.hide();
						tr.hide();
					}
				}else{
					pointGraphic.hide();
					txtGraphic.hide();
					pointValGraphic.hide();
					tr.hide();
				}
				waterDataTBody.append(tr);
				layer.add(pointGraphic);
				txtLayer.add(txtGraphic);
				valLayer.add(pointValGraphic);
			});
			waterDataTable.data('csvDatas',csvDatas);
			var renderer = new esri.renderer.SimpleRenderer(
					createMarkerSymbol());
			layer.setRenderer(renderer);
			layer.redraw();
			txtLayer.hide();
			valLayer.hide();
			}else{
				for(var i=0;i<5;i++){
					waterDataTable.find('tbody').append('<tr style="height:22px"><td></td><td></td><td></td><td></td></tr>');
				}
			}
		});
		$.map.reExtent();
	}
	
	water.find(':checkbox').not(outSide).on('change',function(){
		var chooseTabsAndIsProv = getWaterChooseTabsAndIsProv();
		var chooseTabs = chooseTabsAndIsProv[0];
		var RRChecked=false;
		for(var i=0;i<chooseTabs.length;i++){
			if(chooseTabs[i]=='RR'){
				RRChecked=true;
				break;
			}
		}
		if(RRChecked==false){
			var checks = reservoirPanelSelect.find(':checkbox');
			$.each(checks,function(i,n){
				$(this).prop('checked',false);
				chooseTabs.splice($.inArray(this.value,chooseTabs),1);
			});
			/*reservoirPanelSelect.hide();*/
		}else{
			reservoirPanelSelect.show();
		}
		var isProv = chooseTabsAndIsProv[1];
		var txtGraphics = txtLayer.graphics;
		var valGraphics = valLayer.graphics;
		
		if(map.graphics.graphics.length!=0){
			var isChoose = $(this).prop('checked');
			var attr = map.graphics.graphics[0].attributes;
			var val = $(this).val();
			if(!isChoose && attr.sttp == val){
				map.graphics.clear();
			}
		}
		
		tableBodyTrs = waterDataTable.find('tbody tr');
		tableBodyFirstTds = waterDataTable.find('.firstTd');
		var addIndex = 0;
		$.each(layer.graphics,function(index,item){
			var attr = item.attributes;
			var sttp = attr.sttp;
			var inProvOrInCity = attr.inProvOrInCity;
			var chooseTabsAndIsProv = getWaterChooseTabsAndIsProv();
			var chooseTabs = chooseTabsAndIsProv[0];
			if($.inArray(sttp,chooseTabs)>=0){
				if((isProv && inProvOrInCity == 0) || inProvOrInCity == 1){
					item.show();
					txtGraphics[index].show();
					valGraphics[index].show();
					var td = tableBodyFirstTds.eq(index);
					td.text(++addIndex);
					td.parent().show();
					tableBodyTrs.eq(index).addClass('filterTr');
				}else{
					item.hide();
					txtGraphics[index].hide();
					valGraphics[index].hide();
					txtLayer.graphics[index].hide();
					tableBodyTrs.eq(index).hide();
					tableBodyTrs.eq(index).removeClass('filterTr');
				}
			}else{
				item.hide();
				txtGraphics[index].hide();
				valGraphics[index].hide();
				tableBodyTrs.eq(index).hide();
				tableBodyTrs.eq(index).removeClass('filterTr');
			}
		});
	});
	
	outSide.on('change',function(){
		var isChoose = $(this).prop('checked');
		var addIndex = 0;
		
		tableBodyTrs = waterDataTable.find('tbody tr');
		tableBodyFirstTds = waterDataTable.find('.firstTd');
		/*waterDataTable.find('.filterTr').removeClass('filterTr');*/
		$('#searchWaterStatInput').val('');
		var chooseTabs = getWaterChooseTabsAndIsProv()[0];
		$.each(layer.graphics,function(index,item){
			var visible = item.visible;
			var inProvOrInCity = item.attributes.inProvOrInCity;
			var val = item.attributes.val;
			var upAndDownVal = item.attributes.upAndDownVal;
			var sttp = item.attributes.sttp;

			if((isChoose && $.inArray(sttp,chooseTabs)>=0) || ($.inArray(sttp,chooseTabs)>=0 && inProvOrInCity == 1)){
				item.show();
				txtLayer.graphics[index].show();
				valLayer.graphics[index].show();
				var td = tableBodyFirstTds.eq(index);
				td.text(++addIndex);
				td.parent().show();
			}else{
				item.hide();
				txtLayer.graphics[index].hide();
				valLayer.graphics[index].hide();
				tableBodyTrs.eq(index).hide();
			}
		});
	});
	
	/*shuikuBox.on('change',function(){
		var isChoose = $(this).prop('checked');
		if(isChoose){
			reservoirPanelSelect.show();
		}else{
			//reservoirPanelSelect.hide();
			var checks = reservoirPanelSelect.find(':checkbox');
			$.each(checks,function(i,n){
				$(this).prop('checked',false);
			});
		}
	});*/
	var graphicHighlight;
	var highlightPointVal;
	waterDataTable.find('tbody').on('click','tr',function(){
		var $_this = $(this);
		waterDataTable.find('.tableTrColor').removeClass('tableTrColor');
		$_this.addClass('tableTrColor');
		
		if($.isEmptyObject($_this.data())){
			return;
		}
		
		var lgtd = $_this.data('lgtd');
		var lttd = $_this.data('lttd');
		var stcd = $_this.data('stcd');
		var stnm = $_this.data('stnm');
		var sttp = $_this.data('sttp');
		var callType = $_this.data('callType');
		var warnLevel=$_this.data('warnLevel');
		highlightPointVal = $_this.data('val');
		upAndDownVal = $_this.data('upAndDownVal');
		var point =  new esri.geometry.Point(lgtd,lttd,map.spatialReference);
		var attr = {
			stcd : stcd,
			stnm : stnm,
			sttp : sttp,
			val : highlightPointVal,
			upAndDownVal : upAndDownVal,
			callType : callType,
			warnLevel:warnLevel
		};
		var graphic = new esri.Graphic(point, null);
		map.centerAndZoom(graphic.geometry,10);
		
		if(graphicHighlight){
			map.graphics.remove(graphicHighlight);
		}
		
		var highlight = new esri.symbol.SimpleMarkerSymbol(
				  esri.symbol.SimpleMarkerSymbol.STYLE_CIRCLE, 22,
				  new esri.symbol.SimpleLineSymbol(
					esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color('#ff0000'), 3.5),
					new dojo.Color([0, 0, 255, 0.01]
				  )
			    );
		graphicHighlight = new esri.Graphic(graphic.geometry, highlight,attr);
		map.graphics.add(graphicHighlight);
		
	});
	function getWaterChooseTabsAndIsProv(){
		var chooseTabs = [];
		water.find(':checked').not('#outSlideWater').each(function(index,item){
			chooseTabs.push($(item).val());
		});
		var isProv = $('#outSlideWater').prop('checked');
		return [chooseTabs,isProv];
	}
	
	$('#queryWaterBtn').on('click',function(){
		var chooseTabsAndIsProv = getWaterChooseTabsAndIsProv();
		var dayTime;
		dayTime=waterPanelDate.find('input[name="dayTimeWater"]').val();
		makerPointers(chooseTabsAndIsProv[0],chooseTabsAndIsProv[1],dayTime);
	});
});